#ifndef QHEXEDIT_P_H
#define QHEXEDIT_P_H

/** \cond docNever */


#include <QtGui>
#include <QtCore/QVector>
#include <QtCore/QPair>
#include "xbytearray.h"
#include "Protocols/ShowableItem.h"

class QHexEditPrivate : public QWidget
{
Q_OBJECT

public:
    QHexEditPrivate(QScrollArea *parent, const ShowableItem *items);

	void setAddressAreaColor(QColor const &color){ _addressAreaColor = color; adjust();}
	QColor addressAreaColor(){return _addressAreaColor;}

	void setData(QByteArray const &data){ _xData.setData(data); adjust();}
	QByteArray data(){return _xData.data();}

	void setSelectionColor(QColor const &color){ _selectionColor = color; adjust();}
	QColor selectionColor(){return _selectionColor;}

	XByteArray & xData(){return _xData;}

	virtual void setFont(const QFont &font){ QWidget::setFont(font); adjust();}
	int setSelection(int begin = -1, int end = 0) ;
signals:
	void mouseClicked(const int begin, const int length); 

private:
    void mousePressEvent(QMouseEvent * event);
    void paintEvent(QPaintEvent *event);
    int cursorPos(QPoint pos);          // calc cursorpos from graphics position. DOES NOT STORE POSITION
	
	//对widget进行调整绘图
	void adjust();
	void ensureVisible()
	{
		// scrolls to cursorx, cusory (which are set by setCursorPos)
		// x-margin is 3 pixels, y-margin is half of charHeight
		_scrollArea->ensureVisible(_cursorX, _cursorY + _charHeight/2, 3, _charHeight/2 + 2);
	}

    void setHexSelection(int begin = -1, int end = 0);         // set min (if below init) or max (if greater init)
	void setAsciiSelection(int begin = -1, int end = 0);

	int getHexSelectionBegin() {return _hexSelectionBegin;}
	int getHexSelectionEnd() {return _hexSelectionEnd;}
	int getAsciiSelectionBegin() {return _asciiSelectionBegin;}
	int getAsciiSelectionEnd() {return _asciiSelectionEnd;}

	QPair<int, int> findRange(int pos);
	void structInfoRange(const ShowableItem *items);

    QColor _addressAreaColor;
    QColor _selectionColor;
    QScrollArea *_scrollArea;

    XByteArray _xData;                      // Hält den Inhalt des Hex Editors
	QVector<QPair<int, int> > _dataRange;

    int _charWidth, _charHeight;            // char dimensions (dpendend on font)
    int _cursorX, _cursorY;                 // graphics position of the cursor
    int _cursorPosition;                    // character positioin in stream (on byte ends in to steps)
    int _xPosAdr, _xPosHex, _xPosAscii;     // graphics x-position of the areas

    int _hexSelectionBegin;                    // First selected char
    int _hexSelectionEnd;                      // Last selected char
    int _hexSelectionInit;                     // That's, where we pressed the mouse button
	int _asciiSelectionBegin;
	int _asciiSelectionEnd;
	int _asciiSlelectionInit;

    int _size;
	int _mousePressArea;
};

/** \endcond docNever */

#endif

